package org.jeecg.modules.flowable.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.flowable.service.ISysDeployFormService;
import org.jeecg.modules.flowable.service.ISysFormService;
import org.jeecg.modules.flowable.domain.dto.SysDeployForm;
import org.jeecg.modules.flowable.domain.dto.SysForm;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * 流程表单Controller
 */
@Slf4j
@RestController
@RequestMapping("/flowable/form")
public class SysFormController {
    @Autowired
    private ISysFormService sysFormService;

    @Autowired
    private ISysDeployFormService sysDeployFormService;

    /**
     * 查询流程表单列表
     */
    @AutoLog(value = "流程表单-分页列表查询")
    @ApiOperation(value = "查询流程表单列表", notes = "查询流程表单列表", response = SysForm.class)
    @GetMapping("/list")
    public Result<IPage<SysForm>> list(SysForm sysForm,
                                       @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                       @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                       HttpServletRequest req) {
        Result<IPage<SysForm>> result = new Result<IPage<SysForm>>();
        QueryWrapper<SysForm> queryWrapper = QueryGenerator.initQueryWrapper(sysForm, req.getParameterMap());
        Page<SysForm> page = new Page<SysForm>(pageNo, pageSize);
        IPage<SysForm> pageList = sysFormService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 导出流程表单列表
     */
    @AutoLog(value = "流程表单-导出")
    @ApiOperation(value = "流程表单导出", notes = "流程表单导出")
    @GetMapping("/export")
    public ModelAndView export(SysForm sysForm) {
        // AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        List<SysForm> list = sysFormService.selectSysFormList(sysForm);
        //导出文件名称
        mv.addObject(NormalExcelConstants.FILE_NAME, "流程表单列表");
        mv.addObject(NormalExcelConstants.CLASS, SysForm.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("流程表单列表数据", "导出人:Jeecg", "导出信息"));
        mv.addObject(NormalExcelConstants.DATA_LIST, list);
        return mv;
    }

    /**
     * 获取流程表单详细信息
     */
    @AutoLog(value = "流程表单-通过id查询")
    @ApiOperation(value = "流程表单-通过id查询", notes = "流程表单-通过id查询")
    @GetMapping(value = "/{formId}")
    public Result<SysForm> getInfo(@PathVariable("formId") Long formId) {
        Result<SysForm> result = new Result<SysForm>();
        SysForm sysForm = sysFormService.getById(formId);
        if (sysForm == null) {
            result.error500("未找到对应实体");
        } else {
            sysForm.setFormId(formId);
            result.setResult(sysForm);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 新增流程表单
     */
    @AutoLog(value = "流程表单-添加")
    @ApiOperation(value = "流程表单-添加", notes = "流程表单-添加")
    @PostMapping
    public Result<SysForm> add(@RequestBody SysForm sysForm) {
        Result<SysForm> result = new Result<SysForm>();
        try {
            sysFormService.insertSysForm(sysForm);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    /**
     * 修改流程表单
     */
    @AutoLog(value = "流程表单-编辑")
    @ApiOperation(value = "流程表单-编辑", notes = "流程表单-编辑")
    @PutMapping
    public Result<SysForm> edit(@RequestBody SysForm sysForm) {
        Result<SysForm> result = new Result<SysForm>();
        SysForm sysPositionEntity = sysFormService.getById(sysForm.getId());
        if (sysPositionEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = sysFormService.updateById(sysForm);
            if (ok) {
                result.success("修改成功!");
            }else {
                result.error500("修改失败");
            }
        }
        return result;
    }

    /**
     * 删除流程表单
     */
    @AutoLog(value = "流程表单-批量删除")
    @ApiOperation(value = "流程表单-批量删除", notes = "流程表单-批量删除")
    @DeleteMapping("/{formIds}")
    public Result<SysForm> remove(@PathVariable Long[] formIds) {
        Result<SysForm> result = new Result<SysForm>();
        if (formIds == null || formIds.length == 0) {
            result.error500("参数不识别！");
        } else {
            this.sysFormService.removeByIds(Arrays.asList(formIds));
            result.success("删除成功!");
        }
        return result;
    }

    /**
     * 挂载流程表单
     */
    @AutoLog(value = "流程表单-挂载流程表单")
    @ApiOperation(value = "流程表单-挂载流程表单", notes = "流程表单-挂载流程表单")
    @PostMapping("/addDeployForm")
    public Result<SysDeployForm> addDeployForm(@RequestBody SysDeployForm sysDeployForm) {
        Result<SysDeployForm> result = new Result<SysDeployForm>();
        try {
            sysDeployFormService.insertSysDeployForm(sysDeployForm);
            result.success("挂载成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("挂载失败");
        }
        return result;
    }
}
